package com.课上案例练习.TreeSet集合;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.*;

/**
 * 底层用到了树算法，二叉树
 * 存储数据是唯一的
 * 如果存储的数据是数字，转换尾对象类型，实现了Comparable接口,并且重写了compareTo方法
 */
public class Homework1 {
    public static void main(String[] args) {

        Student s1 = new Student("貂蝉", 17);
        Student s2 = new Student("貂蝉", 19);
        Student s3 = new Student("王昭君", 22);
        TreeSet<Student> treeSet = new TreeSet();
        //第一种，增加的类已经实现了比较器的比较方法
        treeSet.add(s1);
        treeSet.add(s2);
        System.out.println(treeSet);
        //第二种，通过lammba表达式，调用有参构造方法，重写方法
        TreeSet set = new TreeSet((Object a,Object b)->((((Student)a).getAge()-((Student)b).getAge()==0?(((Student)a).getName().compareTo(((Student)b).getName())):(((Student)a).getAge()-((Student)b).getAge()))));

    }
}

@Data
@AllArgsConstructor
class Student implements Comparable<Student> {
    private String name;
    private int age;


    /**
     * 重写
     * @param o the object to be compared.
     * @return
     */
    @Override
    public int compareTo(Student o) {
        return (this.age-o.age==0?(this.name.compareTo(o.name)):(o.age-this.age));
    }
}